/**
 * 
 */
package com.xbang.attendance.web.action.system.warning;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.system.EntityWarning;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;
import com.xbang.core.utils.DateUtils;

/**
 * @author dev.zhang
 * 
 */
public class WarningManager extends BaseActionSupport {

	private static final long serialVersionUID = -5374111090079794779L;

	private EntityWarning entityWarning;

	@Autowired
	private SystemService systemService;

	/** 仓储 */
	private List<Warehouse> chooseWarehouse = new ArrayList<Warehouse>();

	private List<Product> chooseProduct = new ArrayList<Product>();

	private String defaultWarnNumber;
	
	private Integer id;

	@Action("/system/warning/${id}/edit")
	public String edit() throws Exception {
		if (entityWarning == null && id != null) {
			entityWarning = (EntityWarning) systemService.getObject(EntityWarning.class, id);
		}
		getMoreInfo();
		return "detail";
	}

	@Action("/system/warning/${id}/delete")
	public String delete() throws Exception {

		if (id != null) {
			systemService.delete(EntityWarning.class, id);
		}

		return "r:/system/warning/list";
	}

	@Action("/system/warning/add")
	public String add() throws Exception {
		defaultWarnNumber = "W" + DateUtils.format(Calendar.getInstance().getTime(), "yyyyMMddhhmmss");
		getMoreInfo();
		return "detail";
	}

	@Action("/system/warning/${id}/view")
	public String view() throws Exception {
		if (entityWarning == null && id != null) {
			entityWarning = (EntityWarning) systemService.getObject(EntityWarning.class, id);
		}
		return "detail";
	}

	@Action("/system/warning/save")
	public String save() throws Exception {

		if (null != entityWarning) {
			if (entityWarning.getId() != null) {
				systemService.update(entityWarning);
			} else
				systemService.save(entityWarning);
		}

		return "r:/system/warning/list";
	}

	private void getMoreInfo() throws Exception {
		chooseProduct = systemService.getObjects(Product.class);
		chooseWarehouse = systemService.getObjects(Warehouse.class);

	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the entityWarning
	 */
	public EntityWarning getEntityWarning() {
		return entityWarning;
	}

	/**
	 * @param entityWarning
	 *            the entityWarning to set
	 */
	public void setEntityWarning(EntityWarning entityWarning) {
		this.entityWarning = entityWarning;
	}

	/**
	 * @return the chooseWarehouse
	 */
	public List<Warehouse> getChooseWarehouse() {
		return chooseWarehouse;
	}

	/**
	 * @param chooseWarehouse
	 *            the chooseWarehouse to set
	 */
	public void setChooseWarehouse(List<Warehouse> chooseWarehouse) {
		this.chooseWarehouse = chooseWarehouse;
	}

	/**
	 * @return the chooseProduct
	 */
	public List<Product> getChooseProduct() {
		return chooseProduct;
	}

	/**
	 * @param chooseProduct
	 *            the chooseProduct to set
	 */
	public void setChooseProduct(List<Product> chooseProduct) {
		this.chooseProduct = chooseProduct;
	}

	/**
	 * @param systemService
	 *            the systemService to set
	 */
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	
	/**
	 * @return the defaultWarnNumber
	 */
	public String getDefaultWarnNumber() {
		return defaultWarnNumber;
	}

	/**
	 * @param defaultWarnNumber
	 *            the defaultWarnNumber to set
	 */
	public void setDefaultWarnNumber(String defaultWarnNumber) {
		this.defaultWarnNumber = defaultWarnNumber;
	}

}
